package com.hqyj.bigdata_project.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("commons")
public class CommonsController extends BaseController {


    @GetMapping("/hutool/randomCode")
    //生成验证码
    public void generateRandomCodeImage(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "token",required = true) String token) throws IOException {

        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 48,4,10);

        //图形验证码写出，可以写出到文件，也可以写出到流
        //lineCaptcha.write("d:/line.png");
        //输出code
        //Console.log(lineCaptcha.getCode());
        //验证图形验证码的有效性，返回boolean值
        //lineCaptcha.verify("1234");
        String randomCode = lineCaptcha.getCode();
        System.out.println("生成的验证码：" + randomCode);

        //把证明码保存到redis缓存中。
        redisUtil.set(token,randomCode,5); //5表示，5分钟之后验证码就失效了。

        lineCaptcha.write(response.getOutputStream());
    }
}
